/*
 * Copyright (c)$today.year.month.day
 *  admin:郑程荣.
 *  文件名:WarnInfoActivity.java
 *  项目功能:
 */


package com.jg.myproject.module.warninfo;
import android.graphics.drawable.Drawable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jg.myproject.api.Const;
import com.jg.myproject.api.bean.BaseEntity;
import com.jg.myproject.api.bean.CommonRequestBean;
import com.jg.myproject.api.bean.reponse.CommonReponseBean;
import com.jg.myproject.module.base.BaseActivity;
import com.jg.myproject.module.projectinfo.ProjectListActivity;
import com.jg.myproject.presenter.WarnInfoPresenter;
import com.jg.myproject.pview.ICommonView;
import com.jg.myproject.utils.RxSPTool;
import com.jg.myproject.widget.TableDividerItemDecoration;
import com.zcr.myproject.R;

import java.util.ArrayList;

import butterknife.BindView;
/**
 * Created by llz on 2018/11/19.
 */
public class WarnInfoActivity extends BaseActivity implements ICommonView {
    @BindView(R.id.rv_content)
    RecyclerView mRvContent;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    @BindView(R.id.stv_01)
    SuperTextView mStv01;
    @BindView(R.id.stv_02)
    SuperTextView mStv02;
    @BindView(R.id.stv_03)
    SuperTextView mStv03;
    private ArrayList<CommonReponseBean.CommonReponseChildBean> mBeans;
    private BaseQuickAdapter mBaseQuickAdapter;
    public int mNextRequestPage = 1;
    WarnInfoPresenter mPresenter;
    public boolean mIsRefresh;
    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_warn_info;
    }
    @Override
    protected void initViews() {
        initTitle(true, "报警信息");
        mPresenter = new WarnInfoPresenter(this);
        mSwipeRefreshLayout.setColorSchemeResources(R.color.main_color);
        mRvContent.setLayoutManager(new LinearLayoutManager(this));
        mStv01.setCenterString(RxSPTool.getString(this,Const.PROJECTNAME));
        mStv02.setCenterString(RxSPTool.getString(this,Const.MONITORPARTCODE));
        mStv03.setCenterString(RxSPTool.getString(this,Const.MONITORPARTNAME));
        mRvContent.addItemDecoration(new TableDividerItemDecoration(this));
        mBeans = new ArrayList<CommonReponseBean.CommonReponseChildBean>();
        mBaseQuickAdapter = new BaseQuickAdapter<CommonReponseBean.CommonReponseChildBean, BaseViewHolder>(R.layout.item_warn_info, mBeans) {
            @Override
            protected void convert(BaseViewHolder baseViewHolder, CommonReponseBean.CommonReponseChildBean bean) {
                TextView tv_01 = baseViewHolder.getView(R.id.tv_01);
                TextView tv_02 = baseViewHolder.getView(R.id.tv_02);
                TextView tv_03 = baseViewHolder.getView(R.id.tv_03);
                TextView tv_04 = baseViewHolder.getView(R.id.tv_04);
                TextView tv_05 = baseViewHolder.getView(R.id.tv_05);
                tv_01.setText(baseViewHolder.getAdapterPosition() + 1 + "");
                tv_02.setText(bean.getSensorName());
                tv_03.setText(bean.getSensorCode());
                tv_04.setText(bean.getSectionStartDate());
                tv_05.setText(bean.getSectionState());
                if(bean.getSectionState().equals("正常")){
                    Drawable drawable = getResources().getDrawable(R.drawable.icon_image_green);
                    drawable.setBounds(2, 0, 32, 32);
                    tv_05.setCompoundDrawables(drawable,null,null,null);
                }else  if(bean.getSectionState().equals("预警")){
                    Drawable drawable = getResources().getDrawable(R.drawable.icon_image_yellow);
                    drawable.setBounds(2, 0, 32, 32);
                    tv_05.setCompoundDrawables(drawable,null,null,null);
                }else{
                    Drawable drawable = getResources().getDrawable(R.drawable.icon_image_red);
                    drawable.setBounds(2, 0, 32, 32);
                    tv_05.setCompoundDrawables(drawable,null,null,null);
                }
            }
        };
        mBaseQuickAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mIsRefresh = mNextRequestPage == 1;
                getData();
            }
        }, mRvContent);
        initRefreshLayout(); // 下拉刷新
        mRvContent.setAdapter(mBaseQuickAdapter);
    }

    @Override
    protected void updateViews(boolean isRefresh) {
        mIsRefresh = true;
        getData();
    }
    private void initRefreshLayout() {
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mNextRequestPage = 1;
                mBaseQuickAdapter.setEnableLoadMore(false);//这里的作用是防止下拉刷新的时候还可以上拉加载
                mIsRefresh = true;
                getData();
            }
        });
    }

    private void getData() {
        CommonRequestBean commonRequestBean = new CommonRequestBean();
        commonRequestBean.setGcId(RxSPTool.getString(WarnInfoActivity.this,Const.PROJECTID));
        commonRequestBean.setMonitorPartCode(RxSPTool.getString(WarnInfoActivity.this,Const.MONITORPARTCODE));
        String userId = RxSPTool.getString(WarnInfoActivity.this,Const.USERID);
        commonRequestBean.setUserId(userId);
        mPresenter.getData(commonRequestBean);//获取数据
    }

    @Override
    public void getReponseBeans(BaseEntity<CommonReponseBean> bean) {
        mNextRequestPage++;
        int size = bean.getBeans().get(0).getSectionMonitorList() == null ? 0 : bean.getBeans().get(0).getSectionMonitorList().size();
        if (mIsRefresh) {
            if(size == 0){
            showEmpty();
            }
            mBeans.clear();
            mBeans.addAll(bean.getBeans().get(0).getSectionMonitorList());
            mBaseQuickAdapter.setNewData(mBeans);
            mBaseQuickAdapter.setEnableLoadMore(true);
            mSwipeRefreshLayout.setRefreshing(false);
        } else {
            if (size > 0) {
                mBeans.addAll(bean.getBeans().get(0).getSectionMonitorList());
                mBaseQuickAdapter.addData(mBeans);
            }
           //mSwipeRefreshLayout.setRefreshing(true);
        }
        if (size < Const.PAGE_SIZE) {
            //第一页如果不够一页就不显示没有更多数据布局
            mBaseQuickAdapter.loadMoreEnd(mIsRefresh);
        } else {
            mBaseQuickAdapter.loadMoreComplete();
        }
    }

}
